package com.zzyl.nursing.service.impl;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.common.utils.uuid.IsExist;
import com.zzyl.nursing.domain.*;
import com.zzyl.nursing.dto.CheckInApplyDto;
import com.zzyl.nursing.dto.CheckInContractDto;
import com.zzyl.nursing.dto.CheckInElderDto;
import com.zzyl.nursing.mapper.*;
import com.zzyl.nursing.util.CodeGenerator;
import com.zzyl.nursing.vo.CheckInConfigVo;
import com.zzyl.nursing.vo.CheckInDetailVo;
import com.zzyl.nursing.vo.CheckInElderVo;
import com.zzyl.nursing.vo.ElderFamilyVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.service.ICheckInService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.UUID;

/**
 * 入住Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-07-13
 */
@Service
public class CheckInServiceImpl extends ServiceImpl<CheckInMapper, CheckIn> implements ICheckInService
{
    @Autowired
    private CheckInMapper checkInMapper;
    @Autowired
    private CheckInConfigMapper checkInConfigMapper;

    @Autowired
    private ContractMapper contractMapper;

    @Autowired
    private BedMapper bedMapper;

    @Autowired
    private ElderMapper elderMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 查询入住
     * 
     * @param id 入住主键
     * @return 入住
     */
    @Override
    public CheckIn selectCheckInById(Long id)
    {
        return getById(id);
    }

    /**
     * 查询入住列表
     * 
     * @param checkIn 入住
     * @return 入住
     */
    @Override
    public List<CheckIn> selectCheckInList(CheckIn checkIn)
    {
        return checkInMapper.selectCheckInList(checkIn);
    }

    /**
     * 新增入住
     * 
     * @param checkIn 入住
     * @return 结果
     */
    @Override
    public int insertCheckIn(CheckIn checkIn)
    {
        return save(checkIn) ? 1 : 0;
    }

    /**
     * 修改入住
     * 
     * @param checkIn 入住
     * @return 结果
     */
    @Override
    public int updateCheckIn(CheckIn checkIn)
    {
        return updateById(checkIn) ? 1 : 0;
    }

    /**
     * 批量删除入住
     * 
     * @param ids 需要删除的入住主键
     * @return 结果
     */
    @Override
    public int deleteCheckInByIds(Long[] ids)
    {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除入住信息
     * 
     * @param id 入住主键
     * @return 结果
     */
    @Override
    public int deleteCheckInById(Long id)
    {
        return removeById(id) ? 1 : 0;
    }

    /**
     * 批量入住
     * @param checkInApplyDto
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public AjaxResult apply(CheckInApplyDto checkInApplyDto) {
        // 1.获取身份证号
        String idCardNo = checkInApplyDto.getCheckInElderDto().getIdCardNo();
        LambdaQueryWrapper<CheckIn> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CheckIn::getIdCardNo , idCardNo)
                .eq(CheckIn::getStatus , IsExist.Exist);
        // 2.判断该身份证号是否已入住
        CheckIn checkIn = getOne(queryWrapper);
        if (checkIn != null) {
            throw new RuntimeException("该老人已入住");
        }
        Long bedId = checkInApplyDto.getCheckInConfigDto().getBedId();
        Bed bed = new Bed();
        bed.setId(bedId);
        //3.设置床位状态为已入住
        bed.setBedStatus(IsExist.Exist);
        bedMapper.updateById(bed);
        bed=bedMapper.selectById(bedId);

        //4.添加或修改老人信息
        Long elderId;
        LambdaQueryWrapper<Elder> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(Elder::getIdCardNo,idCardNo);
        Elder elder = elderMapper.selectOne(queryWrapper1);
        if(elder != null){
            elder.setBedId(bedId);
            elder.setStatus(IsExist.Exist);
            elderMapper.updateById(elder);
        }else{
            elder = new Elder();
        CheckInElderDto elder1 = checkInApplyDto.getCheckInElderDto();
        BeanUtils.copyProperties(elder1, elder);
        elder.setBedId(bedId);
        elder.setStatus(IsExist.Exist);
        elderMapper.insert(elder);
        elder=elderMapper.selectOne(queryWrapper1);
        }
        elderId = elder.getId();

        //5.添加签约信息
        CheckInContractDto checkInContractDto = checkInApplyDto.getCheckInContractDto();
        Contract contract = new Contract();
        BeanUtils.copyProperties(checkInContractDto, contract);
        contract.setContractNumber("HT" + CodeGenerator.generateContractNumber());
        contract.setSignDate(LocalDateTime.now());
        contract.setElderId(elderId);
        contract.setStartDate(checkInApplyDto.getCheckInConfigDto().getStartDate());
        contract.setEndDate(checkInApplyDto.getCheckInConfigDto().getEndDate());
        contract.setStatus(IsExist.Exist);
        contract.setElderName(elder.getName());
        contract.setAgreementPath("ssssssssssss");//协议地址
        contractMapper.insert(contract);

        //6.添加入住信息
        CheckIn checkIn1 = new CheckIn();
        BeanUtils.copyProperties(checkInApplyDto.getCheckInConfigDto(), checkIn1);
        checkIn1.setStatus(IsExist.NotExist);
        checkIn1.setElderId(elderId);
        checkIn1.setElderName(elder.getName());
        checkIn1.setBedNumber(bed.getBedNumber());
        checkIn1.setIdCardNo(elder.getIdCardNo());
        checkInMapper.insert(checkIn1);
        LambdaQueryWrapper<CheckIn> queryWrapper2 = new LambdaQueryWrapper<>();
        queryWrapper2.eq(CheckIn::getIdCardNo, idCardNo);
        checkIn = getOne(queryWrapper2);

        //7.添加入住配置信息
        CheckInConfig checkInConfig = new CheckInConfig();
        BeanUtils.copyProperties(checkInApplyDto.getCheckInConfigDto(), checkInConfig);
        checkInConfig.setCheckInId(checkIn.getId());
        checkInConfigMapper.insert(checkInConfig);

        return null;
    }

    /**
     * 详情
     * @param id
     * @return
     */
    @Override
    public CheckInDetailVo detail(Long id) {
        CheckInDetailVo checkInDetailVo = new CheckInDetailVo();
        //1.查询入住配置信息
        CheckInConfig checkInConfig = checkInConfigMapper.selectById(id);
        CheckInConfigVo checkInConfigVo = new CheckInConfigVo();
        BeanUtils.copyProperties(checkInConfig, checkInConfigVo);
        checkInDetailVo.setCheckInConfigVo(checkInConfigVo);

        //2.查询入住信息
        CheckIn checkIn = checkInMapper.selectById(checkInConfigVo.getCheckInId());
        //3.查询老人信息
        CheckInElderVo checkInElderVo = new CheckInElderVo();
        Elder elder = elderMapper.selectById(checkIn.getElderId());
        BeanUtils.copyProperties(elder, checkInElderVo);
        checkInDetailVo.setCheckInElderVo(checkInElderVo);

        //4.查询老人家属信息
        List<ElderFamilyVo> elderFamilyVoList = new ArrayList<>();
        checkInDetailVo.setElderFamilyVoList(elderFamilyVoList);
        //5.查询签约信息
        Contract contract = contractMapper.selectByElderId(elder.getId());
        checkInDetailVo.setContract(contract);
        return checkInDetailVo;
    }

}
